package com;

import com.pojo.Customer;
import com.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class CustomerTest {
    @Test
    public void findCustomerByNameAndJobsTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Customer customer = new Customer();
        customer.setCusername("jack");
        customer.setCjobs("teacher");
        List<Customer> customers = sqlSession.selectList("com.mapper.CustomerMapper.findCustomerByNameAndJobs", customer);
        for (Customer customer2 : customers) {
            System.out.println(customer2);
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void updateCustomerByset(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Customer customer = new Customer();
        customer.setCid(3);
        customer.setCphone("13311111234");
        int rows = sqlSession.update("com.mapper.CustomerMapper.updateCustomerByset", customer);
        if (rows > 0) {
            System.out.println("您成功修改了" + rows +"条数据!");
        }else {
            System.out.println("执行修改操作失败! ! !");
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void findByArrayTest(){
        SqlSession session = MyBatisUtils.getSqlSession();
        //创建数组,封装查询id
        Integer[] roleIds = {2,3};
        //执行SQLSession的查询方法,返回结果集
        List<Customer> customers = session.selectList("com.mapper.CustomerMapper.findByArray", roleIds);
        //输出查询结果信息
        for (Customer customer : customers) {
            //打印输出结果
            System.out.println(customer);
        }
        //关闭SQLSession
        session.commit();
        session.close();
    }
    @Test
    public void findByListTest(){
        SqlSession session = MyBatisUtils.getSqlSession();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        List<Customer> customers = session.selectList("com.mapper.CustomerMapper.findByList", ids);
        for (Customer customer : customers) {
            System.out.println(customer);
        }
        session.commit();
        session.close();
    }
}